package blatt3;

//CannonGame application
//Described in Chapter 6 of
//Understanding Object - Oriented Programming with Java
//by Timothy A Budd
//Published by Addison - Wesley
//
//see ftp://ftp.cs.orst.edu/pub/budd/java/ReadMe.html
//for further information
//
//reformatted and slightly modified. peter, oct 2000

import java.awt.*;

class CannonGame extends Frame {
	public static void main(String[] args) {
		CannonGame world = new CannonGame(Integer.parseInt(args[0]));

		world.show();
	}

	public static final int FrameWidth = 600;
	public static final int FrameHeight = 400;
	private int angle = 0;
	private String message = "";
	private CannonBall cannonBall = null;
	private static final int barrelLength = 30;
	private static final int barrelWidth = 10;

	public CannonGame(int theta) { // resize our frame

		setSize(FrameWidth, FrameHeight);
		setTitle("Cannon Game");

		// set the local variables
		angle = theta;
		message = "Angle = " + angle;

		// create the cannon ball
		double radianAngle = angle * Math.PI / 180.0;
		double sinAngle = Math.sin(radianAngle);
		double cosAngle = Math.cos(radianAngle);

		// create the cannon ball
		cannonBall = new CannonBall(20 + (int) (barrelLength * cosAngle),
				dy(5 + (int) (barrelLength * sinAngle)), 5, 12 * cosAngle, -12
						* sinAngle);
	}

	public static int dy(int y) {
		return FrameHeight - y;
	}

	public void paint(Graphics g) {
		int x = 20;
		// location of cannon
		int y = 5;
		double radianAngle = angle * Math.PI / 180.0;
		int lv = (int) (barrelLength * Math.sin(radianAngle));
		int lh = (int) (barrelLength * Math.cos(radianAngle));
		int sv = (int) (barrelWidth * Math.sin(radianAngle + Math.PI / 2));
		int sh = (int) (barrelWidth * Math.cos(radianAngle + Math.PI / 2));

		// draw cannon
		g.setColor(Color.green);
		g.drawLine(x, dy(y), x + lh, dy(y + lv));
		g.drawLine(x + lh, dy(y + lv), x + lh + sh, dy(y + lv + sv));
		g.drawLine(x + lh + sh, dy(y + lv + sv), x + sh, dy(y + sv));
		g.drawLine(x + sh, dy(y + sv), x, dy(y));
		g.drawOval(x - 8, dy(y + 10), 12, 12);
		// draw target
		g.setColor(Color.red);
		g.fillRoundRect(FrameWidth - 100, dy(12), 50, 10, 6, 6);

		// draw cannonball
		if (cannonBall != null) {
			cannonBall.move();
			cannonBall.paint(g);

			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
			}

			//add coords to message
			String coords = "x:"+cannonBall.x()+" y:"+cannonBall.y();
			System.out.println(coords);
			g.drawString(coords, FrameWidth / 2, (FrameHeight / 2)+15);

			if (dy(cannonBall.y()) > 0){
				repaint();
			} else {
				int targetX = FrameWidth - 100;

				if ((cannonBall.x() > targetX)
						&& (cannonBall.x() < (targetX + 50)))
					message = "You Hit It!";
				else
					message = "Missed!";

				cannonBall = null;
			}
		}

		// draw message
		System.out.println(message);
		g.drawString(message, FrameWidth / 2, FrameHeight / 2);

	}
}
